import request from 'umi-request';
import moment from 'moment';

export async function getInsuranceList(params = {}) {
  return request('/hr-ssc/api/api-admin/insurance/getInsuranceList', {
    method: 'post',
    data: {
      pageNum: params.current,
      ...params,
    },
  }).then((res) => {
    if (res.flag) {
      const data = res.data;
      return {
        current: data.currPage,
        data: data.list,
        pageSize: data.pageSize,
        success: true,
        total: data.totalCount,
      };
    } else {
      return {};
    }
  });
}

export async function saveOrUpdateInsurance(params) {
  return request(`/hr-ssc/api/api-admin/insurance/saveOrUpdateInsurance`, {
    method: 'post',
    data: params,
  }).then((res) => {
    return res;
  });
}

export async function getInsuranceEmployeeInfo(insuranceId) {
  return request(`/hr-ssc/api/api-admin/insurance/getInsuranceEmployeeInfo`, {
    method: 'post',
    data: {insuranceId},
    requestType: 'form'
  }).then((res) => {
    if (res.flag){
      return res.data;
    }
    return [];
  });
}

export async function saveOrUpdateEmployeeSecurity(params) {
  return request(`/hr-ssc/api/api-admin/insurance/saveOrUpdateEmployeeSecurity`, {
    method: 'post',
    data: params,
  }).then((res) => {
    return res;
  });
}

export async function deleteSecurity(insuranceId) {
  return request(`/hr-ssc/api/api-admin/insurance/deleteSecurity`, {
    method: 'post',
    data: {insuranceId},
    requestType: 'form'
  }).then((res) => {
    return res;
  });
}

export async function getMemberRecord(params = {}) {
  let securityStatus = params.type;
  return request('/hr-ssc/api/api-admin/insurance/getMemberRecord', {
    method: 'post',
    data: {
      ...params,
      securityStatus,
      pageNum: params.current,
      pageSize: 2000
    },
  }).then((res) => {
    if (res.flag) {
      const data = res.data;
      return {
        current: data.currPage,
        data: data.list,
        pageSize: data.pageSize,
        success: true,
        total: data.totalCount,
      };
    } else {
      return {};
    }
  });
}

export async function getAllInsuranceList() {
  return request(`/hr-ssc/api/api-admin/insurance/getAllInsuranceList`, {
    method: 'post',
  }).then((res) => {
    if (res.flag){
      return res.data;
    }
    return [];
  });
}


export async function getMemberChange(params = {}) {
  let orgname = params.orgName;
  let startTime = params.operationTime && moment(params.operationTime[0]).format('YYYY-MM-DD');
  let endTime = params.operationTime && moment(params.operationTime[1]).format('YYYY-MM-DD');
  return request('/hr-ssc/api/api-admin/insurance/getMemberChange', {
    method: 'post',
    data: {
      startTime,
      endTime,
      orgname,
      ...params,
      pageNum: params.current,
      pageSize: params.pageSize
    },
  }).then((res) => {
    if (res.flag) {
      const data = res.data;
      return {
        current: data.currPage,
        data: data.list,
        pageSize: data.pageSize,
        success: true,
        total: data.totalCount,
      };
    } else {
      return {};
    }
  });
}

export async function updateInsuranceConfig(status) {
  return request(`/hr-ssc/api/api-admin/insurance/updateInsuranceConfig`, {
    method: 'post',
    data: {status},
    requestType: 'form'
  }).then((res) => {
    return res;
  });
}

export async function getInsuranceConfig() {
  return request(`/hr-ssc/api/api-admin/insurance/getInsuranceConfig`, {
    method: 'post',
  }).then((res) => res);
}
